﻿using Infrastructure.Service;
using Model.Repositories.Interfaces;
using Model.Service;
using OMS.Models;
using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace OMS.Controllers
{
    [AllowAnonymous]
    public class AccountController : Controller
    {
        //AuthorizationProvider prov = new AuthorizationProvider();
        //CustomRoleProvider roles = new CustomRoleProvider();

        private ICustomRoleProvider roles;
        private IAuthorizationProvider prov;
        public AccountController(ICustomRoleProvider roles, IAuthorizationProvider prov)
        {
            this.roles = roles;
            this.prov = prov;
        }

        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                var user = prov.GetUser(model.UserName);
                if (user == null)
                {
                    ModelState.AddModelError("", "Such user does not exist in the system");
                    return View();
                }

                if (Membership.ValidateUser(model.UserName, model.Password))
                {
                    if (prov.IsMaxUsersOnline())
                    {
                        ModelState.AddModelError("", "It is max users online");
                    }
                    else
                    {
                        if (prov.IsUserOnline(model.UserName))
                        {
                            ModelState.AddModelError("", " This user is online already");
                        }
                        else
                        {
                            FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                            var role = roles.GetRoleOfUser(model.UserName);
                            switch (role)
                            {
                                case "Administrator":
                                    return RedirectToAction("Index", "Administration");

                                case "Merchandiser":
                                    return RedirectToAction("Index", "Merchandise");

                                case "Supervisor":
                                    return RedirectToAction("Index", "Item");

                                default:
                                    return RedirectToAction("Index", "Ordering");
                            }
                            
                        }
                    }
                    return RedirectToAction("Index", "Ordering");
                }
                else
                {
                    ModelState.AddModelError("", "Password is incorrect");
                }
            }
            return View(model);
        }

        public ActionResult LogOff()
        {
            prov.LogOfUser(User.Identity.Name);
            FormsAuthentication.SignOut();
            return RedirectToAction("Login", "Account");
        }

        public ActionResult Register()
        {
            return View();
        }

        public void UpdateUserLastActivityDate()
        {
        }

     /*   [HttpPost]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                MembershipUser membershipUser = ((AuthorizationProvider)Membership.Provider).CreateUser(model.Email, model.Password);

                if (membershipUser != null)
                {
                    FormsAuthentication.SetAuthCookie(model.Email, false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    ModelState.AddModelError("", "Ошибка при регистрации");
                }
            }
            return View(model);
        } */

    }
}